<template>
	<view class="content">
        <view class="uni-list">
        	<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item,index) in news" :key="index"
			@tap="openinfo" :data-newsid="item.post_id">
			<!-- @tap为跳转:data为传递一个独立的id -->
        		<view class="uni-media-list">
        			<image class="uni-media-list-logo" :src="item.author_avatar"></image>
        			<view class="uni-media-list-body">
        				<view class="uni-media-list-text-top">{{item.title}}</view>
        				<view class="uni-media-list-text-bottom uni-ellipsis">{{item.created_at}}</view>
        			</view>
        		</view>
        	</view>
        </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				news : []
			};
		},
		onLoad:function(){
			uni.showLoading({
				title:"加载中..."
				// 加载动画
			})
			// 网络请求
			uni.request({
				url: 'https://unidemo.dcloud.net.cn/api/news',
				method: 'GET',
				data: {},
				success: res => {
					this.news=res.data;
					// 把接受的数据返回到news里
					uni.hideLoading();
					// 当页面隐藏时消除动画
				},
				fail: () => {},
				complete: () => {}
			});
		},
		methods:{
			openinfo(e) {
				var newsid = e.currentTarget.dataset.newsid;
				uni.navigateTo({
					url: '../info/info?newsid=' +newsid,
					// 转换到对应的页面ID
				});
			}
		},
	}
</script>

<style>
.uni-media-list-body{height: auto;}
.uni-media-list-text-top{line-height: 1.6em;}
</style>
